package logic

import (
	"context"
	"fmt"
	"post/docker/mysql"

	"post/postrpc/internal/svc"
	"post/postrpc/postrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *postrpc.LoginRequest) (*postrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	//use := cache.Rdb.Get(l.ctx, "sendSms").Val()
	//if use != in.SendSmsCode {
	//	return nil, fmt.Errorf("短信验证码错误")
	//}
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在,请前去注册")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return &postrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
